/* -------------------------------------------------------------------
 * BIE is Copyright 2001-2002 Brunswick Corp.
 * -------------------------------------------------------------------
 * Please read the legal notices (docs/legal.txt) and the license
 * (docs/license.txt) that came with this distribution before using
 * this software.
 * -------------------------------------------------------------------
 */

/** 
 * This class simply takes a set of SBIE auth information from the
 * command-line, and makes a "ping" webservice request with it.
 *
 * It will exit with a successful exit code if the request recieves a
 * response, or a fail code otherwise.  Presumably, failure indicates
 * that the given auth data is invalid or incorrect, but might also be
 * cause by problems (eg, network connectivity) unrelated to
 * authentication.
 *
 * @version $Id: PingSBIE.java,v 1.1 2003/05/20 23:23:34 siewean Exp $ 
 * @author gfast
 */

import java.text.MessageFormat;
import java.net.URL;
import java.io.FileOutputStream;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class PingSBIE {

    public static void main( String[] args ) 
        throws Exception
    {
        if ( args.length < 5 ) {
            throw new IllegalArgumentException("Usage: PingSBIE <hostname> <port> <username> <password> <outputcertFN>");
        }
        String hostname = args[0];
        String hostport = args[1];
        String username = args[2];
        String password = args[3];
        String certOutFN = args[4];
        Object[] userargs = 
            new Object[] { hostname, hostport, username, password };
        System.out.println( 
            MessageFormat.format( 
                "Using:\n" +
                "\thostname = {0}\n" +
                "\thostport = {1}\n" +
                "\tusername = {2}\n" +
                "\tpassword = {3}",
                userargs ) );
        
        String soapEndpoint = 
            MessageFormat.format("http://{0}:{1}/services/SBIEPing", userargs);

        doPing( soapEndpoint, username, password, certOutFN );
        
        System.exit(0); /* Return a successful exit code */
    }

    public static void doPing(
        String soapEndpoint,
        String username, 
        String password,
        String certOutFN )
        throws Exception
    {
        System.out.println("Connecting to " + soapEndpoint + "...");
        
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTargetEndpointAddress( new URL(soapEndpoint) );
        call.setOperationName( "pingForCertificate" );
        call.setReturnType( XMLType.XSD_BASE64 );
        call.setUsername( username );
        call.setPassword( password );
        byte[] ret = (byte[])call.invoke( new Object[] {} );
        System.out.println("OK - saving certificate to " + certOutFN);

        FileOutputStream out = new FileOutputStream( certOutFN );
        out.write( ret );
        out.close();
    }
    
}
